using System;
using System.Linq;
using System.Web;
using System.Web.UI;
using ASPNETVN.PORTAL.Components.Configuration;
using ASPNETVN.PORTAL.Components.Configuration.Items;
using ASPNETVN.PORTAL.Components.Security;
using ASPNETVN.PORTAL.Components.UI;

namespace ASPNETVN.PORTAL
{
    public partial class Default : Components.UI.Page
    {
        protected override void OnInit(EventArgs e)
        {
            // Control ID.
            if (Request.QueryString["cid"] != null)
            {
                ContentPane.Attributes.Add("class", "VirtualModuleContent");
                ContentPane.Controls.Add(Page.LoadControl(string.Format("~/Controls/{0}.ascx", Request.QueryString["cid"])));
                LeftPane.Visible = false;
                RightPane.Visible = false;
            }
            // Share control ID.
            else if (!string.IsNullOrEmpty(Request.QueryString["sid"]))
            {
                ContentPane.Attributes.Add("class", "VirtualModuleContent");
                ContentPane.Controls.Add(Page.LoadControl(string.Format("~/Components/Share/Controls/{0}.ascx", Request.QueryString["sid"])));
                LeftPane.Visible = false;
                RightPane.Visible = false;
            }
            // Add / Edit Control ID.
            else if (Request.QueryString["aecid"] != null)
            {
                if (Request.QueryString["mid"] != null)
                {
                    // Obtain SiteSettings from Current Context
                    PortalDataSet ds = PortalConfiguration.GetPortalDataSet();

                    // Find the appropriate Module in the Module table
                    PortalDataSet.ModuleRow moduleRow = ds.Module.FindByID(new Guid(Request.QueryString["mid"]));

                    PortalDataSet.ModuleDefinitionRow moduledefineRow = ds.ModuleDefinition.FindByID(moduleRow.ModuleDefinitionID);
                    string sourceFile = moduledefineRow.SourceFile;
                    // Remove 'Main.ascx'
                    sourceFile = sourceFile.Substring(0, sourceFile.LastIndexOf('/'));
                    // Remove '~/'
                    sourceFile = sourceFile.Substring(2);
                    ContentPane.Attributes.Add("class", "VirtualModuleContent");
                    ContentPane.Controls.Add(Page.LoadControl(string.Format("~/{0}/Controls/{1}.ascx", sourceFile, Request.QueryString["aecid"])));
                }
                LeftPane.Visible = false;
                RightPane.Visible = false;
            }
            // Setting Control ID.
            else if (Request.QueryString["amcid"] != null)
            {
                ContentPane.Attributes.Add("class", "VirtualModuleContent");
                ContentPane.Controls.Add(Page.LoadControl(string.Format("~/Administration/Module/{0}.ascx", Request.QueryString["amcid"])));
                LeftPane.Visible = false;
                RightPane.Visible = false;
            }
            // Normal Control ID.
            else if (Request.QueryString["ncid"] != null)
            {
                if (Request.QueryString["mid"] != null)
                {
                    // Obtain SiteSettings from Current Context
                    PortalDataSet ds = PortalConfiguration.GetPortalDataSet();

                    // Find the appropriate Module in the Module table
                    PortalDataSet.ModuleRow moduleRow = ds.Module.FindByID(new Guid(Request.QueryString["mid"]));

                    PortalDataSet.ModuleDefinitionRow moduledefineRow = ds.ModuleDefinition.FindByID(moduleRow.ModuleDefinitionID);
                    string sourceFile = moduledefineRow.SourceFile;
                    // Remove 'Main.ascx'
                    sourceFile = sourceFile.Substring(0, sourceFile.LastIndexOf('/'));
                    // Remove '~/'
                    sourceFile = sourceFile.Substring(2);
                    ContentPane.Attributes.Add("class", "VirtualModuleContent");
                    ContentPane.Controls.Add(Page.LoadControl(string.Format("~/{0}/Controls/{1}.ascx", sourceFile, Request.QueryString["ncid"])));
                }
                LeftPane.Visible = false;
                RightPane.Visible = false;
            }
            else
            {
                // Obtain Portal from Current Context.
                Portal portal = (Portal)HttpContext.Current.Items["Portal"];
                LeftPane.Visible = portal.ActiveTab.ShowLeftPane;
                RightPane.Visible = portal.ActiveTab.ShowRightPane;

                // Ensure that the visiting user has access to the current page.
                if (PortalSecurity.IsInRoles(portal.ActiveTab.AuthorizedRoles) == false)
                {
                    PortalSecurity.AccessDenied();
                }

                #region Load show every where modules

                // BEGIN: List of show every where modules.
                PortalDataSet ds = PortalConfiguration.GetPortalDataSet();
                foreach (PortalDataSet.ModuleRow moduleRow in ds.Module.OrderBy(x => x.SortOrder))
                {
                    // Pass show every where modules.
                    bool isExist = false;
                    foreach (Module m in portal.ActiveTab.Modules)
                    {
                        if (m.ID == moduleRow.ID)
                        {
                            isExist = true;
                            break;
                        }
                    }

                    if (isExist) continue;

                    // Viewable.
                    if (PortalSecurity.HasViewAuthorization(moduleRow.ID) || PortalSecurity.IsInRole("1"))
                    {
                        if (moduleRow.ShowEveryWhere)
                        {
                            Control parent;
                            switch (moduleRow.PaneName)
                            {
                                case "LeftPane":
                                    parent = LeftPane;
                                    break;
                                case "RightPane":
                                    parent = RightPane;
                                    break;
                                default:
                                    parent = ContentPane;
                                    break;
                            }

                            Module module = Module.LoadModuleRow(moduleRow);
                            module.TabID = this.TabID;

                            PortalModuleControl portalModule = (PortalModuleControl)Page.LoadControl(moduleRow.ModuleDefinitionRow.SourceFile);
                            portalModule.Module = module;
                            parent.Controls.Add(portalModule);
                        }
                    }
                }
                // END: List of show every where modules.
                #endregion

                // Dynamically Populate the Left, Center and Right pane sections of the portal page.
                if (portal.ActiveTab.Modules.Count > 0)
                {
                    // Loop through each entry in the configuration system for this tab.
                    foreach (Module module in portal.ActiveTab.Modules.OrderBy(x => x.SortOrder))
                    {
                        bool isViewableModule = PortalSecurity.HasViewAuthorization(module.ID) || PortalSecurity.IsInRole("1");
                        if (!isViewableModule) continue;
                        //---------------

                        Control parent;
                        switch (module.PaneName)
                        {
                            case "LeftPane":
                                parent = LeftPane;
                                break;
                            case "RightPane":
                                parent = RightPane;
                                break;
                            default:
                                parent = ContentPane;
                                break;
                        }

                        // If no caching is specified, create the user control instance and dynamically
                        // inject it into the page.  Otherwise, create a cached module instance that
                        // may or may not optionally inject the module into the tree
                        
                        if (module.CacheTimeOut == 0)
                        {
                            // Check module visible.
                            if (isViewableModule)
                            {
                                PortalModuleControl portalModule = (PortalModuleControl)Page.LoadControl(module.SourceFile);
                                portalModule.Module = module;
                                parent.Controls.Add(portalModule);
                            }
                        }
                        else
                        {
                            // Check module visible.
                            if (isViewableModule)
                            {
                                CachedPortalModuleControl portalModule = new CachedPortalModuleControl();
                                portalModule.ModuleConfiguration = module;
                                parent.Controls.Add(portalModule);
                            }
                        }
                        parent.Visible = true;
                    }
                }
            }

            base.OnInit(e);
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            
        }
    }
}